const utils = require('./utils')

let _route = require('./route')
export let Route = _route


export let _ = utils
let { nio } = require('./nio')

let lastFlyio

export function initRequest(conf = {}) {
  lastFlyio = nio.createFlyio()
  return nio.initRequest(lastFlyio, conf)
}

export let net = {}
net.encodeQueryData = function (data, encode = false) {
  let ret = []
  for (let d in data) {
    if (encode) {
      ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]))
    } else {
      ret.push(d + '=' + data[d])
    }
  }
  return ret.join('&')
}


let _wx = require('./wx').default

export let wx = _wx

let _regexp = {}
_regexp.mobile = function (labelName) {
  return function(rule, value, callback, source, options) {
    let errors = [];
    if (!value) {
      errors.push(
        new Error(`${labelName ? labelName : rule.field}为空`)
      )
      return errors
    }
    let trimValue = value.trim()
    let intValue = parseInt(trimValue)
    if (isNaN(intValue)) {
      errors.push(
        new Error(`${labelName ? labelName : rule.field}格式错误`)
      )
    }
    if(!/^1{1}[0-9]{10}$/.test(value)) {
      errors.push(
        new Error(`${labelName ? labelName :rule.field}是1开头的11位数字`)
      );
    }
    return errors;
  }
}

_regexp.required = function (type = 'string', message) {
  if (message) {
    return {
      type: type,
      required: true,
      message,
    }
  } else {
    return {
      type: type,
      required: true,
    }
  }
}

_regexp.strRequired = function (labelName) {
  return function(rule, value, callback, source, options) {
    let errors = [];
    // console.log(rule, value)
    if (!value) {
      errors.push(
        new Error(`${labelName ? labelName : rule.field}为空`)
      )
      return errors;
    }
    let trimValue = value.trim()
    if (!trimValue || trimValue.length < 1) {
      errors.push(
        new Error(`${labelName ? labelName : rule.field}为空`)
      )
    }
    return errors;
  }
}

export let Regexp = _regexp

export let wxe = _wx

let _reg = {}
_reg.required = function (opt) {
  let d = {
    required: true,
  }
  return {
    ...d,
    ...opt,
  }
}
_reg.stringType = function (opt) {
  let d = {
    type: 'string',
    required: true,
  }
  return {
    ...d,
    ...opt,
  }
}
_reg.isOnlyHasNum = function (message = 'only can has num') {
  return {
    pattern: /^[0-9]+$/,
    message: 'only can has num ',
  }
}
_reg.isPhone = function (message = 'invalid phone') {
  return [
    {
      pattern: /^1[0-9]{10}$/,
      message: 'wrong phone format',
    },
  ]
}
_reg.lenRange = function (min = 0, max = 1, message = 'invalid range') {
  return function (rule, value, callback, source, options) {
    let errors = []
    let strValue = value + ''
    if (strValue.length < min || strValue.length > max) {
      errors.push(
        {
          message: `invalid range min:${min} max:${max}`,
          field: rule.field,
        },
      )
    }
    callback(errors)
  }
}

export let reg = _reg

export function simpleRequired(keys = []) {
  let ret = {}
  keys.forEach(key => {
    if (typeof key === 'string') {
      ret[key] = [
        _reg.required(),
      ]
    }
    if (Array.isArray(key)) {
      ret[key[0]] = key[1]
    }
  })
  return ret
}

/**
 * format url
 *
 * @param url
 * @param args
 * @example
 *
 * formatUrl('/student/course/:uuid/intention/', {
 *   uuid: 1
 * })
 * @returns {*}
 */
export let formatUrl = utils._formatUrl

export function rpx(v) {
  if (wx.getSystemInfoSync) {
    const systemInfo = wx.getSystemInfoSync()
    return v / 750 * systemInfo.windowWidth
  }
  return v
}

export function transformCitys(data, options = {}) {
  return data.map(function (province) {
    if (options.customProvince) {
      options.customProvince(province)
    } else {
      province.label = province.name
      province.value = province.uuid
    }
    province.children = province.citys.map(function (city) {
      if (options.customCity) {
        options.customCity(city)
      } else {
        city.label = city.name
        city.value = city.uuid
      }
      return city
    })
    return province
  })
}
